<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Models\ActivityPrize;
use Illuminate\Support\Facades\DB;

class Kernel extends ConsoleKernel
{
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();
        $schedule->command('dfjk:sync-user-actived-at')->dailyAt('00:00')->onOneServer();

        $limits = [ActivityPrize::LIMIT_DAILY, ActivityPrize::LIMIT_MONTHLY, ActivityPrize::LIMIT_WEEKLY, ActivityPrize::LIMIT_YEARLY];
        foreach (DB::table('activity_prizes')->whereIn('limit', $limits)->cursor() as $row) {
            $method = $row->limit;
            $schedule->call(function () use ($row) {
                $row->stock = $row->limit_value;
                $row->save();
                //$row->increment('stock', $row->limit_value);
            })->$method()->name($row->name)->onOneServer();
        }
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__ . '/Commands');

        require base_path('routes/console.php');
    }
}
